package com.github.McPlugin.util;

import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider;

import java.util.UUID;

import static org.bukkit.Bukkit.getServer;

public class VaultUtil
{
    public static Economy economy = null;
    public static boolean setupEconomy()
    {
        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
        if(economyProvider != null)
        {
            economy = economyProvider.getProvider();
        }
        return (economy != null);
    }

    public static boolean give(UUID playerUUID,double price)
    {
        OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerUUID);
        return economy.depositPlayer(offlinePlayer,price).transactionSuccess();
    }

    public static double seeMoney(UUID playerUUID)
    {
        OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerUUID);
        return economy.getBalance(offlinePlayer);
    }

    public static boolean pay(UUID playerUUID,double price)
    {
        OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerUUID);
        return economy.has(offlinePlayer,price) && economy.withdrawPlayer(offlinePlayer,price).transactionSuccess();
    }

}
